package com.cow.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cow.pojo.OrderGood;
import com.cow.pojo.Orders;
import com.cow.pojo.UserAddress;
import com.cow.service.OrderGoodService;
import com.cow.service.OrderService;
import com.cow.service.UserService;
import com.cow.util.OrderUtilBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author 光环and鬼泣
 * @version 2018年6月5日 上午10:45:02
 */
@Api(description = "订单接口")
@Controller
public class OrderController {

	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderGoodService orderGoodService;
	@Autowired
	private UserService userService;

	@ApiOperation(value = "获取订单列表", notes = "后台界面接口,分页")
	@ResponseBody
	@RequestMapping(value = "/getOrderList.action", method = RequestMethod.POST)
	public JSONObject getOrderList(HttpServletRequest req, HttpServletResponse rep, @RequestParam("page") String page)
			throws Exception {
		List<Orders> orderList = orderService.getOrderList();
		List<OrderUtilBean> cuuentOrderList = new ArrayList<OrderUtilBean>();
		int totalPage = orderList.size() / 20 + 1;
		for (int i = 0; i < orderList.size(); i++) {
			if (i >= (Integer.parseInt(page) - 1) * 20 && i < (Integer.parseInt(page) * 20)) {
				cuuentOrderList.add(new OrderUtilBean(orderList.get(i)));
			}
		}
		for (int i = 0; i < cuuentOrderList.size(); i++) {
			List<OrderGood> orderGoodList = orderGoodService.getOrderGoodList(cuuentOrderList.get(i).getOrderId());
			cuuentOrderList.get(i).setOrderGoodList(orderGoodList);
			UserAddress userAddress = userService.getUseraddress(cuuentOrderList.get(i).getAddressId());
			cuuentOrderList.get(i).setUserAddress(userAddress);
		}
		JSONObject data = new JSONObject();
		data.put("totalPage", totalPage);
		data.put("page", page);
		data.put("orderList", JSONArray.fromObject(cuuentOrderList));
		return data;
	}

	@ApiOperation(value = "获取订单列表", notes = "前端界面接口,不分页")
	@ResponseBody
	@RequestMapping(value = "/getUserOrderList.action", method = RequestMethod.POST)
	public JSONObject getUserOrderList(HttpServletRequest req, HttpServletResponse rep,
			@RequestParam("userId") String userId) throws Exception {
		List<Orders> orderList = orderService.getOrderList(userId);
		List<OrderUtilBean> orderUtilBeanList = new ArrayList<OrderUtilBean>();
		for (Orders orders : orderList) {
			orderUtilBeanList.add(new OrderUtilBean(orders));
		}
		for (int i = 0; i < orderUtilBeanList.size(); i++) {
			List<OrderGood> orderGoodList = orderGoodService.getOrderGoodList(orderUtilBeanList.get(i).getOrderId());
			orderUtilBeanList.get(i).setOrderGoodList(orderGoodList);
			UserAddress userAddress = userService.getUseraddress(orderUtilBeanList.get(i).getAddressId());
			orderUtilBeanList.get(i).setUserAddress(userAddress);
		}
		JSONObject data = new JSONObject();
		data.put("orderList", JSONArray.fromObject(orderUtilBeanList));
		return data;
	}

	@ApiOperation(value = "搜索订单", notes = "订单号搜索")
	@ResponseBody
	@RequestMapping(value = "/getOrderById.action", method = RequestMethod.POST)
	public JSONObject getOrderById(HttpServletRequest req, HttpServletResponse rep,
			@RequestParam("orderId") String orderId) throws Exception {
		Orders order = orderService.getOrder(orderId);
		OrderUtilBean orderUtilBean = new OrderUtilBean(order);
		List<OrderGood> orderGoodList = orderGoodService.getOrderGoodList(order.getOrderId());
		orderUtilBean.setOrderGoodList(orderGoodList);
		UserAddress userAddress = userService.getUseraddress(order.getAddressId());
		orderUtilBean.setUserAddress(userAddress);
		JSONObject data = new JSONObject();
		data.put("order", JSONArray.fromObject(orderUtilBean));
		return data;
	}

}
